//Accepted
#include <cstdio>
#include <algorithm>
using namespace std;
int p[200001], rank[200001];
void make_set(int x){
	p[x]=x;
	rank[x]=0;
}
void link(int x, int y){
	if(rank[x]>rank[y]){
		p[y]=x;
	}
	else{
		p[x]=y;
		if(rank[x]==rank[y]){
			rank[y]++;
		}
	}
}
int find_set(int x){
	if(x!=p[x]){
		p[x]=find_set(p[x]);
	}
	return p[x];
}
void union_set(int x, int y){
	link(find_set(x),find_set(y));
}
typedef struct eeeee{
	int x, y, z;
}edge;

edge e[200001];
int n, m, i, j, sum, ss;
bool comp(edge a, edge b){
	return a.z<b.z;
}
int main(){
	//freopen("in.txt","r",stdin);
	scanf("%d%d", &n, &m);
	while(n+m){
		ss=0;
		for(i=0;i<m;++i){
			scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z);
			ss+=e[i].z;
			++e[i].x;++e[i].y;
			}
		
		for(i=1;i<=n;++i)make_set(i);
		sort(e,e+m, comp);
		//for(i=0;i<m;++i)printf("%d %d %d\n", e[i].x, e[i].y, e[i].z);
		sum=0;
		for(i=0;i<m;++i){
			if(find_set(e[i].x)!=find_set(e[i].y)){
				sum+=e[i].z;
				union_set(e[i].x, e[i].y);
			}
		}
		printf("%d\n", ss-sum);
		scanf("%d%d", &n, &m);
	}
	return 0;
}
